package pers.dzk.fivelnarow.checkerboard;

import pers.dzk.fivelnarow.config.MainWindowConfig;

import javax.swing.*;
import java.awt.*;
import java.io.Serializable;

/**
 * @author 丁泽凯
 */
public class Checkerboard extends CheckerboardSuper  implements Serializable {

    public Checkerboard(){
        super();
    }
    public Checkerboard(ImageIcon img){
        super(img);
    }
    @Override
    public void initial(JPanel jPanel){
        //设置棋盘大小
        jPanel.setPreferredSize(new Dimension(getWidth(),getHeight()));
        //设置背景颜色
        jPanel.setBackground(new Color(204,162,129));
        //添加背景图片
        if(getImg() != null){
            getImg().setImage(getImg().getImage().getScaledInstance(MainWindowConfig.getWidth(), MainWindowConfig.getHeight(), Image.SCALE_DEFAULT));
            JLabel label = new JLabel(getImg());
            jPanel.add(label);
        }
    }

    @Override
    public void paintCheckerboard(Graphics2D g) {
        //画棋盘
        for (int i = 0; i < getLineCount(); i++){
            //x轴绘画
            g.drawLine(getStart().x, getStart().y-i* getLineDistance(), getStart().x+ getLineDistance() * getLineCount() - getLineDistance(), getStart().y-i* getLineDistance());
            //y轴绘画
            g.drawLine(getStart().x+i* getLineDistance(), getStart().y, getStart().x+i* getLineDistance(), getStart().y- getLineDistance() * getLineCount() + getLineDistance());
        }
        //画棋盘上的棋子
        for (int i = 0; i < getLineCount(); i++) {
            for (int j = 0; j < getLineCount(); j++) {
                getPieceMatrix()[i][j].paint(g);

            }
        }
    }

}
